#!/bin/sh
#**************************************************************
# Copyright (C) 2006-2009 Hewlett-Packard Development Company, L.P.
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# version 2 as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
# ***************************************************************
PROG=$(basename $0)

DATA=_strings.data
PASS=checkstr.OK
WC=$(wc -l < $DATA) || exit 1
ERRS=0
# echo "$PROG: validating phrase seeds in license strings..."
for i in $(seq 1 $WC) ; do
	LINE=$(head -n $i $DATA | tail -n 1)
	if echo "${LINE}" | egrep -iq "^\[=NULL=\]" ; then
		continue
	fi
	KEY=$(echo "$LINE" | awk -F\" '{print $2}')
# License string can contain `:`
	REST=$(expr "$LINE" : '\[.*\] _.*: \(.*\)$')
	TAG=$(expr "$LINE" : '\[.*\] \(.*\): ')
#	echo LINE $i: key $KEY tag $TAG rest $REST
	if [ -f checkstr.OK ]; then
#		grep -qx "$TAG" $PASS && echo "$TAG: OK" && continue
		grep -qx "$TAG" $PASS && continue
	fi
##	printf "$LINE\nLINE %03d: " $i
	if echo "$REST" | egrep -iq "$KEY" ; then
		X=1	# echo "OK (egrep)"
	elif echo "$REST" | fgrep -iq "$KEY" ; then
		X=1	# echo "OK (fgrep)"
	else
		CH1=$(echo $KEY | cut -c1)
		OTH=$(echo $KEY | cut -c2-)
		KLUDGE="${CH1})${OTH}"
		if echo "$LINE" | fgrep -iq "$KLUDGE" ; then
			X=1	# echo "OK (kludge \"$KLUDGE\")"
		else
			echo "$PROG: Key \"$KEY\" NOT FOUND in entry below:"
			echo "$LINE"
			ERRS=$((ERRS+1))
		fi
	fi
done
if [ $ERRS -eq 0 ] ; then
	WILD=$(egrep '(=SOME=|=ANY=|=FEW=)' $DATA | wc -l)
	printf "$PROG: %d phrases, %d contain wild-cards\n" $WC $WILD
	exit 0
fi
echo "$PROG: Error count = $ERRS"
exit 1
